package com.jrx.anytxn.param.controller.investor;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.investor.PrInvestorInstallmentFeeReq;
import com.jrx.anytxn.param.dto.investor.PrInvestorInstallmentFeeRes;
import com.jrx.anytxn.param.service.investor.IPrInvestorInstallmentFeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * 出资方分期手续费表
 *
 * @author 黄秋平
 * @date 2020-5-20 15:16:17
 */
@Api(tags = "页面出资方分期手续费参数API接口")
@RestController
@RequestMapping(value = "/param/investor/installments")
public class PrInvestorInstallmentFeeController {

    private static final Logger logger = LoggerFactory.getLogger(PrInvestorInstallmentFeeController.class);

    @Resource
    IPrInvestorInstallmentFeeService iPrInvestorInstallmentFeeService;

    @Resource
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID和费用ID查询出资方分期手续费表信息", notes = "根据租户ID和费用ID查询出资方分期手续费表信息")
    @GetMapping(value = "/{tenantId}/{investorInstallmentFeeId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "investorInstallmentFeeId", value = "出资方分期手续费ID", required = true)
    })
    public TxnRespResult<PrInvestorInstallmentFeeRes> getByTenIdAndFeeId(@NotNull @PathVariable String tenantId, @PathVariable String investorInstallmentFeeId) {
        TxnRespResult<PrInvestorInstallmentFeeRes> result = new TxnRespResult<>();
        PrInvestorInstallmentFeeRes res;
        try {
            res = iPrInvestorInstallmentFeeService.findByTenIdAndFeeId(String.valueOf(infoService.getUser().getProjectId()), investorInstallmentFeeId);
                if (res.getChargePercent() != null && res.getChargePercent().compareTo(BigDecimal.valueOf(0)) != 0) {
                    res.setChargePercent(res.getChargePercent().multiply(BigDecimal.valueOf(100)));
                }
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询出资方分期手续费表信息", notes = "分页查询出资方分期手续费表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrInvestorInstallmentFeeRes>> getAllPageByTenId(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<PrInvestorInstallmentFeeRes>> result = new TxnRespResult<>();
        TxnPage<PrInvestorInstallmentFeeRes> response;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            response = iPrInvestorInstallmentFeeService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增出资方分期手续费表信息", notes = "新增出资方分期手续费表信息")
    @PostMapping("/")
    public TxnRespResult<PrInvestorInstallmentFeeRes> createInvestorInstallmentFeeTable(@RequestBody PrInvestorInstallmentFeeReq installmentFeeTableReq) {
        PrInvestorInstallmentFeeRes res;
        TxnRespResult<PrInvestorInstallmentFeeRes> result = new TxnRespResult<>();
        try {
            installmentFeeTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            installmentFeeTableReq.setUpdateBy(infoService.getUser().getUserName());
            if (installmentFeeTableReq.getChargePercent() != null && installmentFeeTableReq.getChargePercent().compareTo(BigDecimal.ZERO) != 0) {
                installmentFeeTableReq.setChargePercent(installmentFeeTableReq.getChargePercent().divide(BigDecimal.valueOf(100)));
            }
            res = iPrInvestorInstallmentFeeService.addInstallmentFeeTable(installmentFeeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID编辑出资方分期手续费表信息", notes = "根据租户ID编辑出资方分期手续费表信息")
    @PutMapping("/")
    public TxnRespResult<PrInvestorInstallmentFeeRes> modifyInvestorInstallmentFeeTable(@RequestBody PrInvestorInstallmentFeeReq installmentFeeTableReq) {
        TxnRespResult<PrInvestorInstallmentFeeRes> result = new TxnRespResult<>();
        PrInvestorInstallmentFeeRes res;
        try {
            installmentFeeTableReq.setUpdateBy(infoService.getUser().getUserName());
            if (installmentFeeTableReq.getChargePercent() != null && installmentFeeTableReq.getChargePercent().compareTo(BigDecimal.ZERO) != 0) {
                installmentFeeTableReq.setChargePercent(installmentFeeTableReq.getChargePercent().divide(BigDecimal.valueOf(100)));
            }
            res = iPrInvestorInstallmentFeeService.modifyInstallmentFeeTable(installmentFeeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和费用ID删除出资方分期手续费表信息", notes = "根据租户ID和费用ID删除出资方分期手续费表信息")
    @DeleteMapping(value = "/{tenantId}/{investorInstallmentFeeId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "investorInstallmentFeeId", value = "出资方分期手续费ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndFeeId(@NotNull @PathVariable String tenantId, @PathVariable String investorInstallmentFeeId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            flag = iPrInvestorInstallmentFeeService.removeByTenIdAndFeeId(String.valueOf(infoService.getUser().getProjectId()), investorInstallmentFeeId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

}
